package com.zh.freechat.domain.user.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zh.freechat.domain.user.entity.ApplyFriend;
import com.zh.freechat.domain.user.entity.UserRelationBlack;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * @author Zhou
 * @date 20:22 2024/3/31
 */
@Mapper
public interface ApplyFriendRecordRepository extends BaseMapper<ApplyFriend> {
    /**
     * 最近的添加好友请求
     *
     * @param applyUserId 申请人用户ID
     * @param userId      用户ID
     */
    @Select(value = "SELECT * FROM ch_apply_friend WHERE user_id=#{userId} AND apply_user_id=#{applyUserId} AND closed=false ")
    ApplyFriend getActiveApplyRecord(@Param("applyUserId") String applyUserId, @Param("userId") String userId);

    /**
     * 查询未处理的添加好友请求
     *
     * @param userId
     * @return
     */
    @Select(value = "SELECT * FROM ch_apply_friend WHERE user_id=#{userId} AND closed=false ")
    List<ApplyFriend> findUnsettledApplyRecordList(@Param("userId") String userId);


    /**
     * 关闭此user全部的好友请求
     *
     * @param userId
     * @return
     */
    @Update(value = "UPDATE ch_apply_friend SET closed=true WHERE user_id=#{userId}")
    List<UserRelationBlack> closeAllByUserId(@Param("userId") String userId);

    /**
     * 关闭这个好友请求
     *
     * @param id
     */
    @Update(value = "UPDATE ch_apply_friend SET closed=true WHERE id=#{id}")
    void closeOne(@Param("id") Long id);

}
